package edu.predatorwars.client.visualization;


import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.EmptyNinePatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Container;
import com.badlogic.gdx.scenes.scene2d.actors.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ComboBox;
import com.badlogic.gdx.scenes.scene2d.ui.ComboBox.ComboBoxStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ToggleButton;
import com.badlogic.gdx.scenes.scene2d.ui.ToggleButton.ToggleButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.nButton;
import com.badlogic.gdx.scenes.scene2d.ui.nList;
import com.badlogic.gdx.scenes.scene2d.ui.nListStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Pane;
import com.badlogic.gdx.scenes.scene2d.ui.Pane.PaneStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
import com.badlogic.gdx.scenes.scene2d.ui.nPane;
import com.badlogic.gdx.scenes.scene2d.ui.nScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.nToggleButton;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.tablelayout.Cell;
import com.badlogic.gdx.scenes.scene2d.ui.tablelayout.TableLayout;

import edu.predatorwars.shared.models.Creature;
import edu.predatorwars.shared.models.GameInfo;

public class StartGameRenderer implements ApplicationListener, StartGameRendererInterface {
	public VisualizationStarter visualizationStarter;
	private InputProcessor input;
	private Actor select_first;
	
	
	
	/*3d stuff - do not dispose */
	private Mesh bear, gorilla, wolf, snake, elephant;
	
	DataSet<Integer, Mesh>selected;
	
	
	private ShaderProgram toonShader;
	private Texture toon;
	private Camera cam;
	private Matrix4  mvMatrix;
	private BitmapFont font_small;
	private BitmapFont font_large;
	float[] material = {127f/255, 0.0f, 0.0f} ;
	
	
	/*dispose this stuff*/
	private Mesh eyeball;
	
	private Texture windowBackground, blank,blankAlpha, blankZero, blankWhite, blankWA, tex_join, tex_create, tex_join_inv, tex_create_inv, tex_cancel, tex_cancel_inv, tex_scroll, tex_bear,
					tex_downArrow, tex_bearDraw, tex_bearDraw_inv, tex_elephantDraw, tex_elephantDraw_inv, tex_gorillaDraw, tex_gorillaDraw_inv, tex_snakeDraw, tex_snakeDraw_inv, tex_wolfDraw, tex_wolfDraw_inv,
					tex_select, tex_select_inv, tex_observe, tex_observe_inv;
	
	private Pixmap  pixba, pixw, pixwa;
	List<Pixmap> cpixmaps;
	List<Texture> ctextures;
	
	private Stage stage;
	
	
	private nList game_list;
	private Window select_game_window;
	private nScrollPane select_game_pane;
	private nPane button_pane, button_pane_createcancel, super_pane, create_pane, player_pane, waiting_pane;
	private nButton btn_joinGame, btn_createGame, btn_cancel, btn_select, btn_observe;
	private nToggleButton btn_bear, btn_elephant, btn_gorilla, btn_snake, btn_wolf;
	private com.badlogic.gdx.scenes.scene2d.ui.Label lbl_stats;
	TextField game_name;
	ComboBox maxPlayers;
	
	List<nToggleButton> ntoggles;
	List<nToggleButton> ctoggles;
	
	
	private List<UUID> game_uuids;
	private List<String> game_names;
	private List<Creature> creatures;
	List<String> map_names = null;
	
	public StartGameRenderer(VisualizationStarter vstart) {
		visualizationStarter = vstart;
		input = new input();
		ntoggles= new ArrayList<nToggleButton>();
		creatures = new ArrayList<Creature>();
	}
	
	public StartGameRenderer(VisualizationStarter vstart, List<String> Map_names) {
	
		visualizationStarter = vstart;
		input = new input();
		map_names = Map_names;
		ntoggles = new ArrayList<nToggleButton>();
		creatures = new ArrayList<Creature>();
	}

	
	

	public void create() {
		
		/* load meshes and 3d stuff*/
		 gorilla = SharedVisResources.gorilla;
		 bear = SharedVisResources.bear;
		 wolf = SharedVisResources.wolf;
		 snake = SharedVisResources.snake;
		 elephant = SharedVisResources.elephant;
		 toon = SharedVisResources.toon;
		 toonShader = SharedVisResources.toonShader;
		 cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		
		 Gdx.gl20.glEnable(GL20.GL_DEPTH_TEST);
		 cam.position.z += 1.5f;
		 cam.position.y += 0.29f;
		 cam.position.x -= 0.105f;
		 cam.update();
		
		 mvMatrix = new Matrix4().setToRotation(1, 0, 0, -64);
		
		font_small = SharedVisResources.font_small;//new BitmapFont();
		font_small.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.MipMapLinearLinear);
		font_large = SharedVisResources.font_large;
		windowBackground = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/blank_pane.png"));
		
		blank = new Texture(1,1,Format.RGB888);
		blankZero = new Texture(1,1,Format.RGBA8888);
		pixba = new Pixmap(1,1,Format.RGBA8888);
		pixba.setColor(0.1f,0.1f,0.1f,0.65f);
		pixba.drawPixel(0,0);
		
		pixw = new Pixmap(1,1,Format.RGBA8888); 
		pixw.setColor(1f,1f,1f,1f);
		pixw.drawPixel(0,0);
		
		pixwa = new Pixmap(1,1,Format.RGBA8888);
		Color wa = new Color(1f,1f,1f,0.6f);
		pixwa.setColor(wa);
		pixwa.drawPixel(0, 0);
		font_small.setColor(wa);
		blankWhite = new Texture(pixw);
		blankAlpha = new Texture(pixba);
		blankWA = new Texture(pixwa);
		
		tex_scroll = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/scroll_block.png"));
		tex_join = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/join_button.png"));
		tex_create = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/create_button.png"));
		tex_join_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/join_button_inv.png"));
		tex_create_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/create_button_inv.png"));
		tex_cancel = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/cancel_button.png"));
		tex_cancel_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/cancel_button_inv.png"));
		tex_bear = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/bear.jpg"));
		tex_downArrow = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/down_arrow.png"));
		tex_bearDraw = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/bear_drawing.jpg"));
		tex_bearDraw_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/bear_drawing_inv.png"));
		tex_elephantDraw = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/elephant_drawing.png"));
		tex_elephantDraw_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/elephant_drawing_inv.png"));
		tex_gorillaDraw = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/gorilla_drawing.png"));
		tex_gorillaDraw_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/gorilla_drawing_inv.png"));
		tex_snakeDraw = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/snake_drawing.jpg"));
		tex_snakeDraw_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/snake_drawing_inv.png"));
		tex_wolfDraw = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/wolf_drawing.jpg"));
		tex_wolfDraw_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/wolf_drawing_inv.png"));
		tex_select = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/select_button.png"));
		tex_select_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/select_button_inv.png"));
		tex_observe = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/observe_button.png"));
		tex_observe_inv = new Texture(Gdx.files.internal(SharedVisResources.resource_root_directory + "textures/observe_button_inv.png"));
		
		int width = Gdx.graphics.getWidth();
		int height = Gdx.graphics.getHeight();
		
		
		Gdx.input.setInputProcessor(input);
		
		stage = new Stage(width, height,false);
	
		
		//all buttons and event listeners
		btn_joinGame = new nButton("btn_join","", new ButtonStyle(font_small, Color.BLACK, new NinePatch(tex_join_inv,0,tex_join_inv.getWidth(), 0, tex_join_inv.getHeight()), new NinePatch(tex_join, 0, tex_join.getWidth(), 0, tex_join.getHeight())));
		btn_createGame = new nButton("btn_create", "" , new Button.ButtonStyle(font_small, Color.BLACK, new NinePatch(tex_create_inv, 0, tex_create_inv.getWidth(), 0, tex_create_inv.getHeight()), new NinePatch(tex_create, 0, tex_create.getWidth(), 0, tex_create.getHeight())));
		btn_cancel = new nButton("btn_cancel","", new ButtonStyle(font_small, Color.BLACK, new NinePatch(tex_cancel_inv,0,tex_cancel_inv.getWidth(),0,tex_cancel_inv.getHeight()), new NinePatch(tex_cancel,0,tex_cancel.getWidth(),0,tex_cancel.getHeight())));
		btn_select = new nButton("btn_select","", new ButtonStyle(font_small, Color.BLACK, new NinePatch(tex_select_inv,0,tex_select_inv.getWidth(),0,tex_select_inv.getHeight()), new NinePatch(tex_select,0,tex_select.getWidth(),0,tex_select.getHeight())));
		btn_observe = new nButton("btn_observe","", new ButtonStyle(font_small, Color.BLACK, new NinePatch(tex_observe_inv,0,tex_observe_inv.getWidth(),0 ,tex_observe_inv.getHeight()), new NinePatch(tex_observe,0,tex_observe.getWidth(),0,tex_observe.getHeight())));
		btn_bear = new nToggleButton("btn_bear","",new ToggleButtonStyle(font_small, Color.WHITE, new NinePatch(tex_bearDraw,0,tex_bearDraw.getWidth(),0,tex_bearDraw.getHeight()), new NinePatch(tex_bearDraw_inv,0,tex_bearDraw_inv.getWidth(),0,tex_bearDraw_inv.getHeight()) ));
		btn_elephant = new nToggleButton("btn_elephant","", new ToggleButtonStyle(font_small, Color.WHITE, new NinePatch(tex_elephantDraw, 0, tex_elephantDraw.getWidth(),0,tex_elephantDraw.getHeight()), new NinePatch(tex_elephantDraw_inv,0,tex_elephantDraw_inv.getWidth(),0,tex_elephantDraw.getHeight())));
		btn_gorilla = new nToggleButton("btn_gorilla","", new ToggleButtonStyle(font_small, Color.WHITE, new NinePatch(tex_gorillaDraw, 0, tex_gorillaDraw.getWidth(),0, tex_gorillaDraw.getHeight()), new NinePatch(tex_gorillaDraw_inv,0,tex_gorillaDraw_inv.getWidth(), 0, tex_gorillaDraw_inv.getHeight())));
		btn_snake = new nToggleButton("btn_snake","", new ToggleButtonStyle(font_small, Color.WHITE, new NinePatch(tex_snakeDraw,0, tex_snakeDraw.getWidth(), 0, tex_snakeDraw.getHeight()), new NinePatch(tex_snakeDraw_inv, 0, tex_snakeDraw_inv.getWidth(), 0, tex_snakeDraw_inv.getHeight())));
		btn_wolf = new nToggleButton("btn_wolf", "", new ToggleButtonStyle(font_small, Color.WHITE, new NinePatch(tex_wolfDraw, 0, tex_wolfDraw.getWidth(), 0, tex_wolfDraw.getHeight()), new NinePatch(tex_wolfDraw_inv,0, tex_wolfDraw_inv.getWidth(), 0, tex_wolfDraw_inv.getHeight())));
		btn_gorilla.setPressed(true);
		selected = new DataSet<Integer, Mesh>(Creature.TYPE_GORILLA,gorilla);
		ntoggles.add(btn_bear);
		ntoggles.add(btn_elephant);
		ntoggles.add(btn_gorilla);
		ntoggles.add(btn_snake);
		ntoggles.add(btn_wolf);
	
		btn_joinGame.setClickListener(new ClickListener() {
			public void click(Button button) {
				if (game_uuids.size() > 0)
				visualizationStarter.joinGame(game_uuids.get(game_list.getSelectedIndex()));
			}
		});
		btn_createGame.setClickListener(new ClickListener() {
			public void click(Button button) {
				if (!create_pane.visible) showCreatePane();
				else if (!player_pane.visible) {
					visualizationStarter.createGame(game_name.getText(), Integer.parseInt(maxPlayers.getSelection()));

				}
			}
		});
		btn_cancel.setClickListener(new ClickListener() {
			public void click(Button button) {
				showSelectGamePane();
		
			}
		});
		btn_select.setClickListener(new ClickListener() {
			public void click(Button button) {
				for (int i=0;i<creatures.size();i++) {
					Creature c = creatures.get(i);
					if (c.getCreatureType() == selected.getKey()) {
						visualizationStarter.selectedCreature(new DataSet<Creature, Float[]>(c, new Float[] {material[0], material[1], material[2]}));
						break;
					}
				}
			}
		});
		btn_observe.setClickListener(new ClickListener() {
			public void click(Button button) {
				if (game_uuids.size() > 0)
				visualizationStarter.observeGame(game_uuids.get(game_list.getSelectedIndex()));
			}
		});
		
		btn_bear.setClickListener(new ToggleButton.ClickListener() {
			public void click(ToggleButton button, boolean isPressed) {
				selected.set(Creature.TYPE_BEAR,bear);
				enforceRadioBehaviorOnToggleButtons(button);
				
			}
		});
		btn_elephant.setClickListener(new ToggleButton.ClickListener() {
			public void click(ToggleButton button, boolean isPressed) {
				selected.set(Creature.TYPE_ELEPHANT,elephant);
				enforceRadioBehaviorOnToggleButtons(button);
				
			}
		});
		btn_gorilla.setClickListener(new ToggleButton.ClickListener() {
			public void click(ToggleButton button, boolean isPressed) {
				selected.set(Creature.TYPE_GORILLA,gorilla);
				enforceRadioBehaviorOnToggleButtons(button);
				
			}
		});
		btn_snake.setClickListener(new ToggleButton.ClickListener() {
			public void click(ToggleButton button, boolean isPressed) {
				selected.set(Creature.TYPE_SNAKE,snake);
				enforceRadioBehaviorOnToggleButtons(button);
				
			}
		});
		btn_wolf.setClickListener(new ToggleButton.ClickListener() {
			public void click(ToggleButton button, boolean isPressed) {
				selected.set(Creature.TYPE_WOLF, wolf);
				enforceRadioBehaviorOnToggleButtons(button);
				
			}
		});
		
		/*Background image*/
		Image ibear = new Image("tb",new TextureRegion(tex_bear,0,0,tex_bear.getWidth(),tex_bear.getHeight()));
		ibear.y = -55;
		stage.addActor(ibear);
		
		/*super pane encompasses everything except background image*/
		super_pane = new nPane("super",stage, width, height, new PaneStyle(new NinePatch(blankAlpha,0,0,0,0))); 
		super_pane.padding(240, 0, 0, 0);
		stage.addActor(super_pane);
		
		/*setup button pane*/
		button_pane = new nPane("parent_pane",stage,0,1,new PaneStyle(new NinePatch(blankAlpha,0,1,0,1)));
		Container btn_container = new Container("btn_container",0,0);
		btn_container.width = 500;
		btn_container.padding(0, 50, 0, 0);
		btn_container.add(btn_createGame);
		btn_container.row();
		btn_container.add(btn_select);
		btn_container.row();
		btn_container.add(btn_joinGame);
		btn_container.row();
		btn_container.add(btn_observe);
		btn_container.row();
		btn_container.add(btn_cancel);
		btn_cancel.Show(false);
		btn_select.Show(false);
		button_pane.add(btn_container);
		button_pane.padding(0, 90, 0, 15);		
		super_pane.add(button_pane);
		
		
		
		
		/*setup select game pane*/
		game_list = new nList("game_list",new String[] {},new nListStyle(font_small, Color.BLACK,new Color(0.6f, 0.2f, 0.2f,1f),new NinePatch(blankWhite,50,10,5,5)));
		Pane c = new Pane("s_pane",stage,0,0,new PaneStyle(new NinePatch(blankAlpha,0,1,0,1)));
		c.add(game_list);
		select_first = select_game_pane = new nScrollPane("select_pane", stage,c, 350, height/2, new ScrollPaneStyle(new NinePatch(blankAlpha,0,0,0,0),new NinePatch(blank,0,0,0,0),new NinePatch(blank,0,0,0,0),new NinePatch(blankAlpha,0,0,0,0), new NinePatch(tex_scroll,0,30,0,30)));
		super_pane.add(select_game_pane);
		
		/*create new game pane*/
		create_pane = new nPane("create_pane",stage,350,height/9, new PaneStyle(new NinePatch(blankAlpha,0,250,0,220)));
		Label lbl_gname = new Label("lbl_gname",font_small, "Game Name:    ");
		game_name = new TextField("game_name",200, new TextFieldStyle(font_small,Color.BLACK, new NinePatch(blankAlpha,0,0,0,0), new TextureRegion(blankAlpha,0,0,100,100),new NinePatch(blankWhite,0,0,0,15)));
		Label lbl_maxPlayers =new Label("lbl_maxPlayers",font_small,"Max Players:  ");
		maxPlayers = new ComboBox("maxPlayers", new String[] {"2","3","4","5","6","7","8","9","10"},stage, new ComboBoxStyle(font_small, Color.BLACK, new NinePatch(tex_downArrow,0,60,0,30),new NinePatch(blankWA,0,0,0,0), new NinePatch(blankWhite,0,0,0,0)));
		Label lbl_mapNames = new Label("lbl_mapNames", font_small, "Map Names:   ");
		
		Cell cell;
		(cell = create_pane.layout.cellDefaults).align = TableLayout.LEFT;
		cell.padTop = "10";
		cell.padLeft = "10";
		cell.padBottom = "5";
		create_pane.row();
		create_pane.add(lbl_gname);
		create_pane.add(game_name);
		create_pane.row();
		if (map_names != null) {
			ComboBox mapNames = new ComboBox("mapNames", map_names.toArray(new String[0]), stage, new ComboBoxStyle(font_small, Color.BLACK, new NinePatch( tex_downArrow, 0, 200,0,30), new NinePatch(blankWA,0,0,0,0), new NinePatch(blankWhite,0,0,0,0)));
			create_pane.add(lbl_mapNames);
			create_pane.add(mapNames);
			create_pane.row();
		}
		create_pane.add(lbl_maxPlayers);
		create_pane.add(maxPlayers);
		create_pane.Show(false);
		super_pane.add(create_pane);
		
		/*setup character select pane*/
		cpixmaps = new ArrayList<Pixmap>();
		ctextures = new ArrayList<Texture>();
		ctoggles = new ArrayList<nToggleButton>();
		player_pane = new nPane("player_pane",stage,350,0,new PaneStyle(new NinePatch(blankAlpha,0,0,0,0)));
		Pane p2 = new Pane("p2",stage,350,0,new PaneStyle(new NinePatch(blankWA,0,0,0,0)));
		com.badlogic.gdx.scenes.scene2d.ui.Label prestats = new com.badlogic.gdx.scenes.scene2d.ui.Label("pre", "HP:\nStrength:  \nArmor:\nSight:\nSpeed:\n", new LabelStyle(font_small, Color.WHITE));
		lbl_stats = new com.badlogic.gdx.scenes.scene2d.ui.Label("post","", new LabelStyle(font_small, Color.WHITE));
	
		Container bc = new Container("bc",0,height);
		Container mc = new Container("mc",0,0);
		Container lc = new Container("lc",0,0);
		bc.add(btn_bear);
		bc.row();
		bc.add(btn_elephant);
		bc.row();
		bc.add(btn_gorilla);
		bc.row();
		bc.add(btn_snake);
		bc.row();
		bc.add(btn_wolf);
		for ( float i=127f,j=0f;j<255;j+=22.5f) {
			for ( float k=0f;k<255;k+=22.5f) {
				final float r = i/255; final float g=j/255; final float b=k/255;
					Pixmap p = new Pixmap(1,1,Format.RGB888);
					p.setColor(r, g, b, 1.0f);
					p.drawPixel(0, 0);
					cpixmaps.add(p);
					Texture t = new Texture(p);
					ctextures.add(t);
					nToggleButton nbt = new nToggleButton(""+r+g+b,"", new ToggleButtonStyle(font_small,Color.WHITE,new NinePatch(t,0,20,0,20), new NinePatch(t,0,20,0,20)));
					nbt.setClickListener(new ToggleButton.ClickListener() {
						public void click(ToggleButton button, boolean isPressed) {
							material[0] = r; material[1] = g; material[2] = b;

						}
						
					});
					mc.add(nbt);
		
					
				}
				mc.row();
		}
		lc.padding(20, 500, 0, 200);
		lc.add(prestats);
		lc.add(lbl_stats);
		p2.add(bc);
		player_pane.add(p2);
		player_pane.add(mc);
		player_pane.add(lc);
		player_pane.Show(false);
		super_pane.add(player_pane);
		
		/*waiting_pane*/
		waiting_pane = new nPane("waiting", stage,0,0, new PaneStyle(new NinePatch(blankAlpha,0,0,0,0)));
		Label lbl_waiting = new Label("waiting", font_large, "Waiting...");
		waiting_pane.add(lbl_waiting);
		waiting_pane.padding(10, 490, 10, 530);
		button_pane.add(waiting_pane);
		waiting_pane.Show(false);
		
		visualizationStarter.selectionScreenCreated();
	}

	private void enforceRadioBehaviorOnToggleButtons(ToggleButton clickedButton) {
	if (!clickedButton.isPressed()) {
			clickedButton.setPressed(true);
			return;
		}
		else {
			for (int i=0;i<ntoggles.size();i++) {
				nToggleButton ntb = ntoggles.get(i);
				if (ntb.equals(clickedButton)) continue;
				ntb.setPressed(false);
				
			}
		}
		updateStats();
	}
	
	private void showCreatePane() {
		if (!create_pane.visible) {
			select_game_pane.Show(false);
			create_pane.Show(true);
			btn_joinGame.Show(false);
			btn_observe.Show(false);
			btn_cancel.Show(true);
			super_pane.invalidate();
			visualizationStarter.gameListVisible(false);
		}
	}
	
	private void showSelectGamePane() {
		if (!select_game_pane.visible) {
			select_game_pane.Show(true);
			create_pane.Show(false);
			player_pane.Show(false);
			btn_cancel.Show(false);
			btn_select.Show(false);
			btn_joinGame.Show(true);
			btn_observe.Show(true);
			btn_createGame.Show(true);
			waiting_pane.Show(false);
			super_pane.invalidate();
			visualizationStarter.gameListVisible(true);
		}
	}
	

	private void showPlayerPane() {
		if (!player_pane.visible) {
			select_game_pane.Show(false);
			create_pane.Show(false);
			btn_joinGame.Show(false);
			btn_cancel.Show(true);
			btn_createGame.Show(false);
			btn_observe.Show(false);
			btn_select.Show(true);
			player_pane.Show(true);
			waiting_pane.Show(false);
			super_pane.invalidate();
			visualizationStarter.gameListVisible(false);
		}
	}
	
	private void showWaitingPane() {
		if (!waiting_pane.visible) {
			select_game_pane.Show(false);
			create_pane.Show(false);
			btn_joinGame.Show(false);
			btn_observe.Show(false);
			player_pane.Show(false);
			btn_createGame.Show(false);
			btn_select.Show(false);
			btn_cancel.Show(false);
			waiting_pane.Show(true);
			super_pane.invalidate();
			visualizationStarter.gameListVisible(false);
		}
	}
	
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	Matrix4 rotator = new Matrix4();
	public float angle = 0;
	public void render() {
		GL20 gl = Gdx.gl20;

		gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
		gl.glClearColor(0.075f, 0.075f, 0.075f, 1f);
		gl.glActiveTexture(GL20.GL_TEXTURE0);
		
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	
		if (player_pane.visible) {
			
			gl.glActiveTexture(GL20.GL_TEXTURE1);
			toon.bind();
			toonShader.begin();
			toonShader.setUniformMatrix("pMatrix", cam.combined);
			toonShader.setUniformf("vp", cam.position.x, cam.position.y, cam.position.z, 1.0f);
			toonShader.setUniformi("outline", 1);
		
			toonShader.setUniformf("material", material[0],material[1],material[2],1.0f);
			toonShader.setUniformMatrix("mvMatrix", mvMatrix);
			mvMatrix = new Matrix4().setToRotation(1.5f,0,0,-65).mul( rotator.setToRotation(0,0,120,MathUtils.sinDeg(angle += Gdx.graphics.getDeltaTime()*35)));
			selected.getValue().render(toonShader, GL20.GL_TRIANGLES);
			toonShader.end();
		}
		
	}

	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	public void pause() {
		// TODO Auto-generated method stub
		
	}

	public void dispose() {
		windowBackground.dispose();
		blank.dispose();
		blankAlpha.dispose();
		blankZero.dispose();
		blankWhite.dispose();
		blankWA.dispose();
		tex_join.dispose();
		tex_create.dispose();
		tex_join_inv.dispose();
		tex_create_inv.dispose();
		tex_cancel.dispose();
		tex_cancel_inv.dispose();
		tex_scroll.dispose();
		tex_bear.dispose();
		tex_downArrow.dispose();
		tex_bearDraw.dispose();
		tex_bearDraw_inv.dispose();
		tex_elephantDraw.dispose();
		tex_elephantDraw_inv.dispose();
		tex_gorillaDraw.dispose();
		tex_gorillaDraw_inv.dispose();
		tex_snakeDraw.dispose();
		tex_snakeDraw_inv.dispose();
		tex_wolfDraw.dispose();
		tex_wolfDraw_inv.dispose();
		tex_select.dispose();
		tex_select_inv.dispose();
		tex_observe.dispose();
		tex_observe_inv.dispose();
		pixba.dispose(); 
		pixw.dispose();
		pixwa.dispose();
		for (int i=0;i<cpixmaps.size();i++)
			cpixmaps.get(i).dispose();
		for (int i=0;i<ctextures.size();i++)
			ctextures.get(i).dispose();
	}


	public void showHome(Boolean b) {
		
	}


	public void showGames() {
		showSelectGamePane();
	}

	
	public void showCreatures() {
		showPlayerPane();
	}

	public void showWaiting() {
		showWaitingPane();
		
	}
	
	
	
	
	public void setGames(List<UUID> Game_uuids, List<String> Game_names) {
		
		
		game_uuids = Game_uuids;
		game_names = Game_names;
		
		String[] names = game_names.toArray(new String[0]);
		game_list.setEntries(names);
		game_list.layout();
	
		
		
	}
	

	public void setCreatures(List<Creature> Creatures) {
		
		creatures = Creatures;
		btn_wolf.Show(false);		btn_wolf.setPressed(false);
		btn_bear.Show(false);		btn_bear.setPressed(false);
		btn_elephant.Show(false);	btn_elephant.setPressed(false);
		btn_snake.Show(false);		btn_snake.setPressed(false);
		btn_gorilla.Show(false);	btn_gorilla.setPressed(false);
		
		//set btn visibility based on creatures
		for (int i=0;i<Creatures.size();i++) {
			switch (Creatures.get(i).getCreatureType()) {
			case Creature.TYPE_BEAR : btn_bear.Show(true); break;
			case Creature.TYPE_ELEPHANT : btn_elephant.Show(true); break;
			case Creature.TYPE_GORILLA : btn_gorilla.Show(true); break;
			case Creature.TYPE_SNAKE : btn_snake.Show(true); break;
			case Creature.TYPE_WOLF : btn_wolf.Show(true); break;
			}
		}
		
		switch(Creatures.get(0).getCreatureType()) {
		case Creature.TYPE_BEAR : selected.set(Creature.TYPE_BEAR, bear); btn_bear.setPressed(true); break;
		case Creature.TYPE_ELEPHANT : selected.set(Creature.TYPE_ELEPHANT, elephant); btn_elephant.setPressed(true); break;
		case Creature.TYPE_GORILLA : selected.set(Creature.TYPE_GORILLA, gorilla); btn_gorilla.setPressed(true); break;
		case Creature.TYPE_SNAKE : selected.set(Creature.TYPE_SNAKE, snake); btn_snake.setPressed(true); break;
		case Creature.TYPE_WOLF : selected.set(Creature.TYPE_WOLF, wolf); btn_wolf.setPressed(true); break;
		}
		
		updateStats();
		
		
	}
	
	private void updateStats() {
		Creature c =null;
		
		if (selected != null)
		for (int i=0;i<creatures.size();i++) {
			c= creatures.get(i);
			if (selected.getKey() == c.getCreatureType()) {
				break;
			}
		}
		

		if (c!= null && lbl_stats != null) {
			lbl_stats.setText(
					c.getStat(Creature.STAT_HEALTH) + "  /  " + c.getStat(Creature.STAT_MAX_HEALTH) + "\n" + 
					c.getStat(Creature.STAT_STRENGTH) + "\n" +
					c.getStat(Creature.STAT_ARMOR) + "\n" + 
					c.getStat(Creature.STAT_SIGHT) + "\n" +
					c.getStat(Creature.STAT_SPEED) + "\n");
		}	
	}
	
	public class input implements InputProcessor {


		public boolean keyDown(int keycode) {
			stage.keyDown(keycode);
			return false;
		}


		public boolean keyUp(int keycode) {
			stage.keyUp(keycode);
			return false;
		}


		public boolean keyTyped(char character) {
			stage.keyTyped(character);
			return false;
		}


		public boolean touchDown(int x, int y, int pointer, int button) {
			stage.touchDown(x, y, pointer, button);			
			return false;
		}


		public boolean touchUp(int x, int y, int pointer, int button) {
			stage.touchUp(x, y, pointer, button);
			
			return false;
		}


		public boolean touchDragged(int x, int y, int pointer) {
			stage.touchDragged(x, y, pointer);
			return false;
		}


		public boolean touchMoved(int x, int y) {
			stage.touchMoved(x, y);
			return false;
		}


		public boolean scrolled(int amount) {
			stage.scrolled(amount);
			return false;
		}
		
	}
}